var MAX_ROW_NUMBER = 99999;

var needToJumpToPage = false;
var hasErrorAfterSaving = false;
var needToGoBackPriorSeletedRow = false;
var saveButton;
var currentPage = { NumberPosition: '1' };
var updateData = false;

function RegisterBeforeUnloadPageEvent(window) {
    $(window).bind("beforeunload", function () {
        if (HasDataChangedInPage()) {
            WarningUnsavedata();
            if (hasErrorAfterSaving == true) {
                return 'All data that you have entered will be lost because saving is not success!';
            }
        }
    });
}

function SetLoadOneTimeForGrid(list, flag) {
    $(list).setGridParam({
        loadonce: flag
    });
}

function SetDataTypeForGrid(list, kindOfData) {
    $(list).setGridParam({
        datatype: kindOfData
    });
}

function RegisterOnSelectRowInGrid(list, DoOnSelectRow) {
    $(list).setGridParam({
        onSelectRow: DoOnSelectRow
    });
}

function RegisterOnLoadCompleteInGrid(list, DoOnLoadComplete) {
    $(list).setGridParam({
        loadComplete: DoOnLoadComplete
    });
}

function RegisterOnAfterSaveCellInGrid(list, DoOnAfterSaveCell) {
    $(list).setGridParam({
        afterSaveCell: DoOnAfterSaveCell
    });
}

function SetHeightOfGrid(list, height) {
    $(list).setGridHeight(height);
}
function SetWidthOfGrid(list, width) {
    $(list).setGridWidth(width, false);
}

function SetEditableGrid(list, afterEditCellFunc) {
    $(list).setGridParam({
        cellEdit: true,
        cellsubmit: 'clientArray',
        afterEditCell: afterEditCellFunc
    });
}

function SetSaveEditableGrid(list, afterSaveCellFunc) {
    $(list).setGridParam({
        cellEdit: true,
        cellsubmit: 'clientArray',
        afterSaveCell: afterSaveCellFunc
    });
}

function RegisterOnBeforeEditCell(list, onBeforeEditCellFunc) {
    $(list).setGridParam({
        beforeEditCell: onBeforeEditCellFunc
    });
}

function GetSelectedRowId(list) {
    return $(list).jqGrid('getGridParam', 'selrow');
}

function GetRowId(list, columnIdName) {
    var grid = jQuery(list);
    var sel_id = grid.jqGrid('getGridParam', 'selrow');
    return grid.jqGrid('getCell', sel_id, columnIdName);
}

function GetCellValue(list, rowId, columnIdName) {
    var rowData = jQuery(list).getRowData(rowId);
    return rowData[columnIdName];
}

function ResetAllFlags() {
    MarkNoDataChangedInPage();
    hasErrorAfterSaving = false;
    needToGoBackPriorSeletedRow = false;
    updateData = false;
}

function ResetWhenClearScreen(selectedRowId, FillData) {
    if (HasDataChangedInPage()) {
        WarningUnsavedata();
        // If user changed data and accept save but fail,
        // system will not clear data of input controls
        if (hasErrorAfterSaving == true) {
            return;
        }
    }
    selectedRowId.Id = '-1';
    ShowHideValidationSummary(false);
    FillData();
    ResetAllFlags();
}

function GetPostData(list) {
    return {
        sortName: function () { return $(list).jqGrid('getGridParam', 'sortname'); },
        sortOrder: function () { return $(list).jqGrid('getGridParam', 'sortorder'); },
        rowId: selectedRowId.Id
    };
}


function SetupGrid(list, urlForGetData, GetColumnNames, GetColumnModel, page,
defaultSortName, defaultSortType, defaultSortOrder, selectedRowId, postData, height, width, loadCompleted) {
    $(list).jqGrid({

        url: urlForGetData,
        datatype: "json",
        mtype: "GET",
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        postData: postData ,
        // Specify the column names
        colNames: GetColumnNames(),
        // Configure the columns
        colModel: GetColumnModel(),
        ignoreCase: true,
        loadonce: true, // Load once all data
        rowNum: 10,
        rowList: [10, 20, 30, 100],
        altRows: true,
        pager: page,
        viewrecords: true,
        width: width ? width : 800,
        height: height,
        scrollrows: true,
        sortable: true,
        sortname: defaultSortName,
        sorttype: defaultSortType,
        sortorder: defaultSortOrder,
        shrinkToFit: false,
        async: false,
        loadComplete: function () {
            if (loadCompleted) loadCompleted();
        },
        loadError: function (xhr, st, str) {
            alert('Error occurred when loading grid');
        }
    });

    $(list).jqGrid('navGrid', page, { edit: false, add: false, del: false, refresh: false, search: false });
    //$(list).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}